import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSClient {

    private FileSystem fs;

    @Before
    public void before() throws IOException, InterruptedException {
        // 1 获取文件系统
        // Configuration configuration = new Configuration();
        // 配置在集群上运行
        // configuration.set("fs.defaultFS", "hdfs://hadoop102:9000");
        //        configuration.setInt("dfs.replication",1);
        fs = FileSystem.get(URI.create("hdfs://localhost:9000"), new Configuration(), "bink");
        System.out.println("Before!!!!!!");
    }

    @Test
    public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
        // 2 创建目录
        fs.mkdirs(new Path("/hdfs-demo"));
    }

    @Test
    public void put() throws IOException, InterruptedException {
        fs.copyFromLocalFile(new Path("in/03.txt"), new Path("/in/03.txt"));
    }

    @Test
    public void get() throws IOException, InterruptedException {
        //用这个对象操作文件系统
        fs.copyToLocalFile(new Path("/in/03.txt"), new Path("output/hdfs/03.txt"));
    }

    @Test
    public void rename() throws IOException, InterruptedException {
        fs.rename(new Path("/in/03.txt"), new Path("/in/03-2.txt"));
    }

    @Test
    public void delete() throws IOException {
        boolean delete = fs.delete(new Path("in/03.txt"),true);//recursive:true
        if (delete) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    @Test
    public void ls() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isFile()) {
                System.out.println("以下信息是一个文件的信息");
                System.out.println(fileStatus.getPath());
                System.out.println(fileStatus.getLen());
            } else {
                System.out.println("这是一个文件夹");
                System.out.println(fileStatus.getPath());
            }
        }

    }

    @Test
    public void listFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);

        while (files.hasNext()) {
            LocatedFileStatus file = files.next();

            System.out.println("======================================");
            System.out.println(file.getPath());

            System.out.println("块信息：");
            BlockLocation[] blockLocations = file.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                String[] hosts = blockLocation.getHosts();
                System.out.print("块在");
                for (String host : hosts) {
                    System.out.print(host + " ");
                }
            }

        }
    }

    @After
    public void after() throws IOException {
        System.out.println("\nAfter!!!!!!!!!!");
        fs.close();
    }


}
